﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;
using RemoteGamePad.GamePadServiceReference;
using System.IO.IsolatedStorage;

namespace RemoteGamePad
{
    public partial class MainPage : PhoneApplicationPage
    {
         Accelerometer accelerometer;
         RotateTransform rotateTransform;
         int valTest;
         private int timeCounter;
         GamePadServiceReference.GamePadServiceClient gamePadService;
        // Constructor
        public MainPage()
        {
            valTest = -1;
            timeCounter = 0;
            InitializeComponent();
            SetupAccelerometer();
            SetupRotationTrasform();
            SetupWCFService();
        }

        private void SetupWCFService()
        {
            gamePadService = new GamePadServiceReference.GamePadServiceClient();
            var settings = IsolatedStorageSettings.ApplicationSettings;
            String adress = "127.0.0.1";
            if(settings.Contains("adress"))
            {
                adress = settings["adress"].ToString();
            }
            gamePadService.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://" + adress + ":59361/GamePadService.svc");

        }

        private void SetupAccelerometer()
        {

            accelerometer = new Accelerometer();

            accelerometer.ReadingChanged += OnSensorReadingChanged;

            accelerometer.Start();

        }

        private void OnSensorReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(delegate()
                                                        {
                                                            try
                                                            {
                                                                timeCounter++;
                                                                yTextBlock.Text = "Y: " + e.Y.ToString("0.00");

                                                                double angle = e.Y * 10;
                                                                rotateTransform.Angle = angle;
                                                                if (timeCounter == 10)
                                                                {
                                                                    gamePadService.TransmitAngleAsync(angle);
                                                                    timeCounter = 0;
                                                                }
                                                            }
                                                            catch (Exception ex)
                                                            {
                                                                MessageBox.Show("An error occured.\n Details:"+ex.Message);
                                                                gamePadService.CloseAsync();
                                                            }
                                                            
                                                        });
        }

        private void SetupRotationTrasform()
        {
            rotateTransform = new RotateTransform();
            rotateTransform.CenterX = 0;
            rotateTransform.CenterY = rotationBar.Height/2;
            rotateTransform.Angle = 0;

            rotationBar.RenderTransform = rotateTransform; 
        }

        private void btn_click(object sender, RoutedEventArgs e)
        {
            gamePadService.TransmitAngleAsync(valTest++);
        }

      
        
    }

    
}